{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 柱状图"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 基本柱状图绘制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "    <div class=\"bk-root\">\n",
       "        <a href=\"https://bokeh.org\" target=\"_blank\" class=\"bk-logo bk-logo-small bk-logo-notebook\"></a>\n",
       "        <span id=\"3921\">Loading BokehJS ...</span>\n",
       "    </div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/javascript": [
       "\n",
       "(function(root) {\n",
       "  function now() {\n",
       "    return new Date();\n",
       "  }\n",
       "\n",
       "  const force = true;\n",
       "\n",
       "  if (typeof root._bokeh_onload_callbacks === \"undefined\" || force === true) {\n",
       "    root._bokeh_onload_callbacks = [];\n",
       "    root._bokeh_is_loading = undefined;\n",
       "  }\n",
       "\n",
       "  const JS_MIME_TYPE = 'application/javascript';\n",
       "  const HTML_MIME_TYPE = 'text/html';\n",
       "  const EXEC_MIME_TYPE = 'application/vnd.bokehjs_exec.v0+json';\n",
       "  const CLASS_NAME = 'output_bokeh rendered_html';\n",
       "\n",
       "  /**\n",
       "   * Render data to the DOM node\n",
       "   */\n",
       "  function render(props, node) {\n",
       "    const script = document.createElement(\"script\");\n",
       "    node.appendChild(script);\n",
       "  }\n",
       "\n",
       "  /**\n",
       "   * Handle when an output is cleared or removed\n",
       "   */\n",
       "  function handleClearOutput(event, handle) {\n",
       "    const cell = handle.cell;\n",
       "\n",
       "    const id = cell.output_area._bokeh_element_id;\n",
       "    const server_id = cell.output_area._bokeh_server_id;\n",
       "    // Clean up Bokeh references\n",
       "    if (id != null && id in Bokeh.index) {\n",
       "      Bokeh.index[id].model.document.clear();\n",
       "      delete Bokeh.index[id];\n",
       "    }\n",
       "\n",
       "    if (server_id !== undefined) {\n",
       "      // Clean up Bokeh references\n",
       "      const cmd_clean = \"from bokeh.io.state import curstate; print(curstate().uuid_to_server['\" + server_id + \"'].get_sessions()[0].document.roots[0]._id)\";\n",
       "      cell.notebook.kernel.execute(cmd_clean, {\n",
       "        iopub: {\n",
       "          output: function(msg) {\n",
       "            const id = msg.content.text.trim();\n",
       "            if (id in Bokeh.index) {\n",
       "              Bokeh.index[id].model.document.clear();\n",
       "              delete Bokeh.index[id];\n",
       "            }\n",
       "          }\n",
       "        }\n",
       "      });\n",
       "      // Destroy server and session\n",
       "      const cmd_destroy = \"import bokeh.io.notebook as ion; ion.destroy_server('\" + server_id + \"')\";\n",
       "      cell.notebook.kernel.execute(cmd_destroy);\n",
       "    }\n",
       "  }\n",
       "\n",
       "  /**\n",
       "   * Handle when a new output is added\n",
       "   */\n",
       "  function handleAddOutput(event, handle) {\n",
       "    const output_area = handle.output_area;\n",
       "    const output = handle.output;\n",
       "\n",
       "    // limit handleAddOutput to display_data with EXEC_MIME_TYPE content only\n",
       "    if ((output.output_type != \"display_data\") || (!Object.prototype.hasOwnProperty.call(output.data, EXEC_MIME_TYPE))) {\n",
       "      return\n",
       "    }\n",
       "\n",
       "    const toinsert = output_area.element.find(\".\" + CLASS_NAME.split(' ')[0]);\n",
       "\n",
       "    if (output.metadata[EXEC_MIME_TYPE][\"id\"] !== undefined) {\n",
       "      toinsert[toinsert.length - 1].firstChild.textContent = output.data[JS_MIME_TYPE];\n",
       "      // store reference to embed id on output_area\n",
       "      output_area._bokeh_element_id = output.metadata[EXEC_MIME_TYPE][\"id\"];\n",
       "    }\n",
       "    if (output.metadata[EXEC_MIME_TYPE][\"server_id\"] !== undefined) {\n",
       "      const bk_div = document.createElement(\"div\");\n",
       "      bk_div.innerHTML = output.data[HTML_MIME_TYPE];\n",
       "      const script_attrs = bk_div.children[0].attributes;\n",
       "      for (let i = 0; i < script_attrs.length; i++) {\n",
       "        toinsert[toinsert.length - 1].firstChild.setAttribute(script_attrs[i].name, script_attrs[i].value);\n",
       "        toinsert[toinsert.length - 1].firstChild.textContent = bk_div.children[0].textContent\n",
       "      }\n",
       "      // store reference to server id on output_area\n",
       "      output_area._bokeh_server_id = output.metadata[EXEC_MIME_TYPE][\"server_id\"];\n",
       "    }\n",
       "  }\n",
       "\n",
       "  function register_renderer(events, OutputArea) {\n",
       "\n",
       "    function append_mime(data, metadata, element) {\n",
       "      // create a DOM node to render to\n",
       "      const toinsert = this.create_output_subarea(\n",
       "        metadata,\n",
       "        CLASS_NAME,\n",
       "        EXEC_MIME_TYPE\n",
       "      );\n",
       "      this.keyboard_manager.register_events(toinsert);\n",
       "      // Render to node\n",
       "      const props = {data: data, metadata: metadata[EXEC_MIME_TYPE]};\n",
       "      render(props, toinsert[toinsert.length - 1]);\n",
       "      element.append(toinsert);\n",
       "      return toinsert\n",
       "    }\n",
       "\n",
       "    /* Handle when an output is cleared or removed */\n",
       "    events.on('clear_output.CodeCell', handleClearOutput);\n",
       "    events.on('delete.Cell', handleClearOutput);\n",
       "\n",
       "    /* Handle when a new output is added */\n",
       "    events.on('output_added.OutputArea', handleAddOutput);\n",
       "\n",
       "    /**\n",
       "     * Register the mime type and append_mime function with output_area\n",
       "     */\n",
       "    OutputArea.prototype.register_mime_type(EXEC_MIME_TYPE, append_mime, {\n",
       "      /* Is output safe? */\n",
       "      safe: true,\n",
       "      /* Index of renderer in `output_area.display_order` */\n",
       "      index: 0\n",
       "    });\n",
       "  }\n",
       "\n",
       "  // register the mime type if in Jupyter Notebook environment and previously unregistered\n",
       "  if (root.Jupyter !== undefined) {\n",
       "    const events = require('base/js/events');\n",
       "    const OutputArea = require('notebook/js/outputarea').OutputArea;\n",
       "\n",
       "    if (OutputArea.prototype.mime_types().indexOf(EXEC_MIME_TYPE) == -1) {\n",
       "      register_renderer(events, OutputArea);\n",
       "    }\n",
       "  }\n",
       "\n",
       "  \n",
       "  if (typeof (root._bokeh_timeout) === \"undefined\" || force === true) {\n",
       "    root._bokeh_timeout = Date.now() + 5000;\n",
       "    root._bokeh_failed_load = false;\n",
       "  }\n",
       "\n",
       "  const NB_LOAD_WARNING = {'data': {'text/html':\n",
       "     \"<div style='background-color: #fdd'>\\n\"+\n",
       "     \"<p>\\n\"+\n",
       "     \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n",
       "     \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n",
       "     \"</p>\\n\"+\n",
       "     \"<ul>\\n\"+\n",
       "     \"<li>re-rerun `output_notebook()` to attempt to load from CDN again, or</li>\\n\"+\n",
       "     \"<li>use INLINE resources instead, as so:</li>\\n\"+\n",
       "     \"</ul>\\n\"+\n",
       "     \"<code>\\n\"+\n",
       "     \"from bokeh.resources import INLINE\\n\"+\n",
       "     \"output_notebook(resources=INLINE)\\n\"+\n",
       "     \"</code>\\n\"+\n",
       "     \"</div>\"}};\n",
       "\n",
       "  function display_loaded() {\n",
       "    const el = document.getElementById(\"3921\");\n",
       "    if (el != null) {\n",
       "      el.textContent = \"BokehJS is loading...\";\n",
       "    }\n",
       "    if (root.Bokeh !== undefined) {\n",
       "      if (el != null) {\n",
       "        el.textContent = \"BokehJS \" + root.Bokeh.version + \" successfully loaded.\";\n",
       "      }\n",
       "    } else if (Date.now() < root._bokeh_timeout) {\n",
       "      setTimeout(display_loaded, 100)\n",
       "    }\n",
       "  }\n",
       "\n",
       "\n",
       "  function run_callbacks() {\n",
       "    try {\n",
       "      root._bokeh_onload_callbacks.forEach(function(callback) {\n",
       "        if (callback != null)\n",
       "          callback();\n",
       "      });\n",
       "    } finally {\n",
       "      delete root._bokeh_onload_callbacks\n",
       "    }\n",
       "    console.debug(\"Bokeh: all callbacks have finished\");\n",
       "  }\n",
       "\n",
       "  function load_libs(css_urls, js_urls, callback) {\n",
       "    if (css_urls == null) css_urls = [];\n",
       "    if (js_urls == null) js_urls = [];\n",
       "\n",
       "    root._bokeh_onload_callbacks.push(callback);\n",
       "    if (root._bokeh_is_loading > 0) {\n",
       "      console.debug(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n",
       "      return null;\n",
       "    }\n",
       "    if (js_urls == null || js_urls.length === 0) {\n",
       "      run_callbacks();\n",
       "      return null;\n",
       "    }\n",
       "    console.debug(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n",
       "    root._bokeh_is_loading = css_urls.length + js_urls.length;\n",
       "\n",
       "    function on_load() {\n",
       "      root._bokeh_is_loading--;\n",
       "      if (root._bokeh_is_loading === 0) {\n",
       "        console.debug(\"Bokeh: all BokehJS libraries/stylesheets loaded\");\n",
       "        run_callbacks()\n",
       "      }\n",
       "    }\n",
       "\n",
       "    function on_error(url) {\n",
       "      console.error(\"failed to load \" + url);\n",
       "    }\n",
       "\n",
       "    for (let i = 0; i < css_urls.length; i++) {\n",
       "      const url = css_urls[i];\n",
       "      const element = document.createElement(\"link\");\n",
       "      element.onload = on_load;\n",
       "      element.onerror = on_error.bind(null, url);\n",
       "      element.rel = \"stylesheet\";\n",
       "      element.type = \"text/css\";\n",
       "      element.href = url;\n",
       "      console.debug(\"Bokeh: injecting link tag for BokehJS stylesheet: \", url);\n",
       "      document.body.appendChild(element);\n",
       "    }\n",
       "\n",
       "    for (let i = 0; i < js_urls.length; i++) {\n",
       "      const url = js_urls[i];\n",
       "      const element = document.createElement('script');\n",
       "      element.onload = on_load;\n",
       "      element.onerror = on_error.bind(null, url);\n",
       "      element.async = false;\n",
       "      element.src = url;\n",
       "      console.debug(\"Bokeh: injecting script tag for BokehJS library: \", url);\n",
       "      document.head.appendChild(element);\n",
       "    }\n",
       "  };\n",
       "\n",
       "  function inject_raw_css(css) {\n",
       "    const element = document.createElement(\"style\");\n",
       "    element.appendChild(document.createTextNode(css));\n",
       "    document.body.appendChild(element);\n",
       "  }\n",
       "\n",
       "  \n",
       "  const js_urls = [\"https://cdn.bokeh.org/bokeh/release/bokeh-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-gl-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-widgets-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-tables-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-mathjax-2.4.2.min.js\"];\n",
       "  const css_urls = [];\n",
       "  \n",
       "\n",
       "  const inline_js = [\n",
       "    function(Bokeh) {\n",
       "      Bokeh.set_log_level(\"info\");\n",
       "    },\n",
       "    function(Bokeh) {\n",
       "    \n",
       "    \n",
       "    }\n",
       "  ];\n",
       "\n",
       "  function run_inline_js() {\n",
       "    \n",
       "    if (root.Bokeh !== undefined || force === true) {\n",
       "      \n",
       "    for (let i = 0; i < inline_js.length; i++) {\n",
       "      inline_js[i].call(root, root.Bokeh);\n",
       "    }\n",
       "    if (force === true) {\n",
       "        display_loaded();\n",
       "      }} else if (Date.now() < root._bokeh_timeout) {\n",
       "      setTimeout(run_inline_js, 100);\n",
       "    } else if (!root._bokeh_failed_load) {\n",
       "      console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n",
       "      root._bokeh_failed_load = true;\n",
       "    } else if (force !== true) {\n",
       "      const cell = $(document.getElementById(\"3921\")).parents('.cell').data().cell;\n",
       "      cell.output_area.append_execute_result(NB_LOAD_WARNING)\n",
       "    }\n",
       "\n",
       "  }\n",
       "\n",
       "  if (root._bokeh_is_loading === 0) {\n",
       "    console.debug(\"Bokeh: BokehJS loaded, going straight to plotting\");\n",
       "    run_inline_js();\n",
       "  } else {\n",
       "    load_libs(css_urls, js_urls, function() {\n",
       "      console.debug(\"Bokeh: BokehJS plotting callback run at\", now());\n",
       "      run_inline_js();\n",
       "    });\n",
       "  }\n",
       "}(window));"
      ],
      "application/vnd.bokehjs_load.v0+json": "\n(function(root) {\n  function now() {\n    return new Date();\n  }\n\n  const force = true;\n\n  if (typeof root._bokeh_onload_callbacks === \"undefined\" || force === true) {\n    root._bokeh_onload_callbacks = [];\n    root._bokeh_is_loading = undefined;\n  }\n\n  \n\n  \n  if (typeof (root._bokeh_timeout) === \"undefined\" || force === true) {\n    root._bokeh_timeout = Date.now() + 5000;\n    root._bokeh_failed_load = false;\n  }\n\n  const NB_LOAD_WARNING = {'data': {'text/html':\n     \"<div style='background-color: #fdd'>\\n\"+\n     \"<p>\\n\"+\n     \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n     \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n     \"</p>\\n\"+\n     \"<ul>\\n\"+\n     \"<li>re-rerun `output_notebook()` to attempt to load from CDN again, or</li>\\n\"+\n     \"<li>use INLINE resources instead, as so:</li>\\n\"+\n     \"</ul>\\n\"+\n     \"<code>\\n\"+\n     \"from bokeh.resources import INLINE\\n\"+\n     \"output_notebook(resources=INLINE)\\n\"+\n     \"</code>\\n\"+\n     \"</div>\"}};\n\n  function display_loaded() {\n    const el = document.getElementById(\"3921\");\n    if (el != null) {\n      el.textContent = \"BokehJS is loading...\";\n    }\n    if (root.Bokeh !== undefined) {\n      if (el != null) {\n        el.textContent = \"BokehJS \" + root.Bokeh.version + \" successfully loaded.\";\n      }\n    } else if (Date.now() < root._bokeh_timeout) {\n      setTimeout(display_loaded, 100)\n    }\n  }\n\n\n  function run_callbacks() {\n    try {\n      root._bokeh_onload_callbacks.forEach(function(callback) {\n        if (callback != null)\n          callback();\n      });\n    } finally {\n      delete root._bokeh_onload_callbacks\n    }\n    console.debug(\"Bokeh: all callbacks have finished\");\n  }\n\n  function load_libs(css_urls, js_urls, callback) {\n    if (css_urls == null) css_urls = [];\n    if (js_urls == null) js_urls = [];\n\n    root._bokeh_onload_callbacks.push(callback);\n    if (root._bokeh_is_loading > 0) {\n      console.debug(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n      return null;\n    }\n    if (js_urls == null || js_urls.length === 0) {\n      run_callbacks();\n      return null;\n    }\n    console.debug(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n    root._bokeh_is_loading = css_urls.length + js_urls.length;\n\n    function on_load() {\n      root._bokeh_is_loading--;\n      if (root._bokeh_is_loading === 0) {\n        console.debug(\"Bokeh: all BokehJS libraries/stylesheets loaded\");\n        run_callbacks()\n      }\n    }\n\n    function on_error(url) {\n      console.error(\"failed to load \" + url);\n    }\n\n    for (let i = 0; i < css_urls.length; i++) {\n      const url = css_urls[i];\n      const element = document.createElement(\"link\");\n      element.onload = on_load;\n      element.onerror = on_error.bind(null, url);\n      element.rel = \"stylesheet\";\n      element.type = \"text/css\";\n      element.href = url;\n      console.debug(\"Bokeh: injecting link tag for BokehJS stylesheet: \", url);\n      document.body.appendChild(element);\n    }\n\n    for (let i = 0; i < js_urls.length; i++) {\n      const url = js_urls[i];\n      const element = document.createElement('script');\n      element.onload = on_load;\n      element.onerror = on_error.bind(null, url);\n      element.async = false;\n      element.src = url;\n      console.debug(\"Bokeh: injecting script tag for BokehJS library: \", url);\n      document.head.appendChild(element);\n    }\n  };\n\n  function inject_raw_css(css) {\n    const element = document.createElement(\"style\");\n    element.appendChild(document.createTextNode(css));\n    document.body.appendChild(element);\n  }\n\n  \n  const js_urls = [\"https://cdn.bokeh.org/bokeh/release/bokeh-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-gl-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-widgets-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-tables-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-mathjax-2.4.2.min.js\"];\n  const css_urls = [];\n  \n\n  const inline_js = [\n    function(Bokeh) {\n      Bokeh.set_log_level(\"info\");\n    },\n    function(Bokeh) {\n    \n    \n    }\n  ];\n\n  function run_inline_js() {\n    \n    if (root.Bokeh !== undefined || force === true) {\n      \n    for (let i = 0; i < inline_js.length; i++) {\n      inline_js[i].call(root, root.Bokeh);\n    }\n    if (force === true) {\n        display_loaded();\n      }} else if (Date.now() < root._bokeh_timeout) {\n      setTimeout(run_inline_js, 100);\n    } else if (!root._bokeh_failed_load) {\n      console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n      root._bokeh_failed_load = true;\n    } else if (force !== true) {\n      const cell = $(document.getElementById(\"3921\")).parents('.cell').data().cell;\n      cell.output_area.append_execute_result(NB_LOAD_WARNING)\n    }\n\n  }\n\n  if (root._bokeh_is_loading === 0) {\n    console.debug(\"Bokeh: BokehJS loaded, going straight to plotting\");\n    run_inline_js();\n  } else {\n    load_libs(css_urls, js_urls, function() {\n      console.debug(\"Bokeh: BokehJS plotting callback run at\", now());\n      run_inline_js();\n    });\n  }\n}(window));"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from bokeh.plotting import output_notebook,figure,show\n",
    "output_notebook()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 垂直柱状图\n",
    "\n",
    "* 绘制方法\n",
    "> vbar()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "  <div class=\"bk-root\" id=\"578f2e21-52fc-49fe-9c54-ecdce6e147a1\" data-root-id=\"3922\"></div>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/javascript": [
       "(function(root) {\n",
       "  function embed_document(root) {\n",
       "    \n",
       "  const docs_json = {\"b1f579db-cb1e-4375-9f31-12b38cd32cea\":{\"defs\":[],\"roots\":{\"references\":[{\"attributes\":{\"below\":[{\"id\":\"3931\"}],\"center\":[{\"id\":\"3934\"},{\"id\":\"3938\"}],\"height\":400,\"left\":[{\"id\":\"3935\"}],\"renderers\":[{\"id\":\"3957\"}],\"title\":{\"id\":\"4119\"},\"toolbar\":{\"id\":\"3946\"},\"width\":400,\"x_range\":{\"id\":\"3923\"},\"x_scale\":{\"id\":\"3927\"},\"y_range\":{\"id\":\"3925\"},\"y_scale\":{\"id\":\"3929\"}},\"id\":\"3922\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{\"coordinates\":null,\"group\":null},\"id\":\"4119\",\"type\":\"Title\"},{\"attributes\":{\"source\":{\"id\":\"3953\"}},\"id\":\"3958\",\"type\":\"CDSView\"},{\"attributes\":{},\"id\":\"3936\",\"type\":\"BasicTicker\"},{\"attributes\":{\"overlay\":{\"id\":\"3945\"}},\"id\":\"3941\",\"type\":\"BoxZoomTool\"},{\"attributes\":{\"bottom_units\":\"screen\",\"coordinates\":null,\"fill_alpha\":0.5,\"fill_color\":\"lightgrey\",\"group\":null,\"left_units\":\"screen\",\"level\":\"overlay\",\"line_alpha\":1.0,\"line_color\":\"black\",\"line_dash\":[4,4],\"line_width\":2,\"right_units\":\"screen\",\"syncable\":false,\"top_units\":\"screen\"},\"id\":\"3945\",\"type\":\"BoxAnnotation\"},{\"attributes\":{},\"id\":\"3943\",\"type\":\"ResetTool\"},{\"attributes\":{},\"id\":\"3923\",\"type\":\"DataRange1d\"},{\"attributes\":{\"coordinates\":null,\"formatter\":{\"id\":\"4125\"},\"group\":null,\"major_label_policy\":{\"id\":\"4126\"},\"ticker\":{\"id\":\"3932\"}},\"id\":\"3931\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"3944\",\"type\":\"HelpTool\"},{\"attributes\":{},\"id\":\"4126\",\"type\":\"AllLabels\"},{\"attributes\":{\"axis\":{\"id\":\"3931\"},\"coordinates\":null,\"group\":null,\"ticker\":null},\"id\":\"3934\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"4122\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{},\"id\":\"3932\",\"type\":\"BasicTicker\"},{\"attributes\":{\"coordinates\":null,\"data_source\":{\"id\":\"3953\"},\"glyph\":{\"id\":\"3954\"},\"group\":null,\"hover_glyph\":null,\"muted_glyph\":{\"id\":\"3956\"},\"nonselection_glyph\":{\"id\":\"3955\"},\"view\":{\"id\":\"3958\"}},\"id\":\"3957\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"tools\":[{\"id\":\"3939\"},{\"id\":\"3940\"},{\"id\":\"3941\"},{\"id\":\"3942\"},{\"id\":\"3943\"},{\"id\":\"3944\"}]},\"id\":\"3946\",\"type\":\"Toolbar\"},{\"attributes\":{},\"id\":\"3927\",\"type\":\"LinearScale\"},{\"attributes\":{\"data\":{\"top\":[1.2,2.5,3.7],\"x\":[1,2,3]},\"selected\":{\"id\":\"4128\"},\"selection_policy\":{\"id\":\"4127\"}},\"id\":\"3953\",\"type\":\"ColumnDataSource\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.1},\"fill_color\":{\"value\":\"red\"},\"hatch_alpha\":{\"value\":0.1},\"hatch_color\":{\"value\":\"red\"},\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":\"red\"},\"top\":{\"field\":\"top\"},\"width\":{\"value\":0.5},\"x\":{\"field\":\"x\"}},\"id\":\"3955\",\"type\":\"VBar\"},{\"attributes\":{},\"id\":\"3939\",\"type\":\"PanTool\"},{\"attributes\":{},\"id\":\"4128\",\"type\":\"Selection\"},{\"attributes\":{},\"id\":\"4127\",\"type\":\"UnionRenderers\"},{\"attributes\":{},\"id\":\"3925\",\"type\":\"DataRange1d\"},{\"attributes\":{\"fill_color\":{\"value\":\"red\"},\"hatch_color\":{\"value\":\"red\"},\"line_color\":{\"value\":\"red\"},\"top\":{\"field\":\"top\"},\"width\":{\"value\":0.5},\"x\":{\"field\":\"x\"}},\"id\":\"3954\",\"type\":\"VBar\"},{\"attributes\":{},\"id\":\"4125\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"coordinates\":null,\"formatter\":{\"id\":\"4122\"},\"group\":null,\"major_label_policy\":{\"id\":\"4123\"},\"ticker\":{\"id\":\"3936\"}},\"id\":\"3935\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"3929\",\"type\":\"LinearScale\"},{\"attributes\":{},\"id\":\"4123\",\"type\":\"AllLabels\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.2},\"fill_color\":{\"value\":\"red\"},\"hatch_alpha\":{\"value\":0.2},\"hatch_color\":{\"value\":\"red\"},\"line_alpha\":{\"value\":0.2},\"line_color\":{\"value\":\"red\"},\"top\":{\"field\":\"top\"},\"width\":{\"value\":0.5},\"x\":{\"field\":\"x\"}},\"id\":\"3956\",\"type\":\"VBar\"},{\"attributes\":{\"axis\":{\"id\":\"3935\"},\"coordinates\":null,\"dimension\":1,\"group\":null,\"ticker\":null},\"id\":\"3938\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"3940\",\"type\":\"WheelZoomTool\"},{\"attributes\":{},\"id\":\"3942\",\"type\":\"SaveTool\"}],\"root_ids\":[\"3922\"]},\"title\":\"Bokeh Application\",\"version\":\"2.4.2\"}};\n",
       "  const render_items = [{\"docid\":\"b1f579db-cb1e-4375-9f31-12b38cd32cea\",\"root_ids\":[\"3922\"],\"roots\":{\"3922\":\"578f2e21-52fc-49fe-9c54-ecdce6e147a1\"}}];\n",
       "  root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\n",
       "\n",
       "  }\n",
       "  if (root.Bokeh !== undefined) {\n",
       "    embed_document(root);\n",
       "  } else {\n",
       "    let attempts = 0;\n",
       "    const timer = setInterval(function(root) {\n",
       "      if (root.Bokeh !== undefined) {\n",
       "        clearInterval(timer);\n",
       "        embed_document(root);\n",
       "      } else {\n",
       "        attempts++;\n",
       "        if (attempts > 100) {\n",
       "          clearInterval(timer);\n",
       "          console.log(\"Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing\");\n",
       "        }\n",
       "      }\n",
       "    }, 10, root)\n",
       "  }\n",
       "})(window);"
      ],
      "application/vnd.bokehjs_exec.v0+json": ""
     },
     "metadata": {
      "application/vnd.bokehjs_exec.v0+json": {
       "id": "3922"
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 画布\n",
    "p = figure(plot_width=400,plot_height=400)\n",
    "# 绘制柱状图\n",
    "p.vbar(x=[1,2,3],\n",
    "       width=0.5,\n",
    "      bottom=0,\n",
    "      top=[1.2,2.5,3.7],\n",
    "      color=\"red\"\n",
    ")\n",
    "# 显示\n",
    "show(p)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 水平的柱状图\n",
    "* 绘制方法\n",
    ">hbar()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {},
   "outputs": [],
   "source": [
    "??p.hbar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "  <div class=\"bk-root\" id=\"2c072704-decb-414f-bd45-d049226b290b\" data-root-id=\"4185\"></div>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/javascript": [
       "(function(root) {\n",
       "  function embed_document(root) {\n",
       "    \n",
       "  const docs_json = {\"344867c2-1680-4cdb-b608-90f313afe6d5\":{\"defs\":[],\"roots\":{\"references\":[{\"attributes\":{\"below\":[{\"id\":\"4196\"}],\"center\":[{\"id\":\"4199\"},{\"id\":\"4203\"}],\"height\":400,\"left\":[{\"id\":\"4200\"}],\"renderers\":[{\"id\":\"4222\"}],\"title\":{\"id\":\"4186\"},\"toolbar\":{\"id\":\"4211\"},\"width\":400,\"x_range\":{\"id\":\"4188\"},\"x_scale\":{\"id\":\"4192\"},\"y_range\":{\"id\":\"4190\"},\"y_scale\":{\"id\":\"4194\"}},\"id\":\"4185\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{\"coordinates\":null,\"formatter\":{\"id\":\"4397\"},\"group\":null,\"major_label_policy\":{\"id\":\"4398\"},\"ticker\":{\"id\":\"4201\"}},\"id\":\"4200\",\"type\":\"LinearAxis\"},{\"attributes\":{\"coordinates\":null,\"group\":null,\"text\":\"\\u6c34\\u5e73\\u67f1\\u72b6\\u56feDemo\"},\"id\":\"4186\",\"type\":\"Title\"},{\"attributes\":{\"axis\":{\"id\":\"4196\"},\"coordinates\":null,\"group\":null,\"ticker\":null},\"id\":\"4199\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"4204\",\"type\":\"PanTool\"},{\"attributes\":{},\"id\":\"4403\",\"type\":\"Selection\"},{\"attributes\":{},\"id\":\"4192\",\"type\":\"LinearScale\"},{\"attributes\":{},\"id\":\"4194\",\"type\":\"LinearScale\"},{\"attributes\":{},\"id\":\"4397\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{},\"id\":\"4197\",\"type\":\"BasicTicker\"},{\"attributes\":{},\"id\":\"4207\",\"type\":\"SaveTool\"},{\"attributes\":{},\"id\":\"4400\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"overlay\":{\"id\":\"4210\"}},\"id\":\"4206\",\"type\":\"BoxZoomTool\"},{\"attributes\":{},\"id\":\"4398\",\"type\":\"AllLabels\"},{\"attributes\":{\"tools\":[{\"id\":\"4204\"},{\"id\":\"4205\"},{\"id\":\"4206\"},{\"id\":\"4207\"},{\"id\":\"4208\"},{\"id\":\"4209\"}]},\"id\":\"4211\",\"type\":\"Toolbar\"},{\"attributes\":{\"coordinates\":null,\"formatter\":{\"id\":\"4400\"},\"group\":null,\"major_label_policy\":{\"id\":\"4401\"},\"ticker\":{\"id\":\"4197\"}},\"id\":\"4196\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"4401\",\"type\":\"AllLabels\"},{\"attributes\":{\"data\":{\"right\":[1.2,2.5,3.7],\"y\":[1,2,3]},\"selected\":{\"id\":\"4403\"},\"selection_policy\":{\"id\":\"4402\"}},\"id\":\"4218\",\"type\":\"ColumnDataSource\"},{\"attributes\":{\"axis\":{\"id\":\"4200\"},\"coordinates\":null,\"dimension\":1,\"group\":null,\"ticker\":null},\"id\":\"4203\",\"type\":\"Grid\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.1},\"fill_color\":{\"value\":\"navy\"},\"hatch_alpha\":{\"value\":0.1},\"hatch_color\":{\"value\":\"navy\"},\"height\":{\"value\":0.5},\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":\"navy\"},\"right\":{\"field\":\"right\"},\"y\":{\"field\":\"y\"}},\"id\":\"4220\",\"type\":\"HBar\"},{\"attributes\":{},\"id\":\"4209\",\"type\":\"HelpTool\"},{\"attributes\":{\"source\":{\"id\":\"4218\"}},\"id\":\"4223\",\"type\":\"CDSView\"},{\"attributes\":{\"coordinates\":null,\"data_source\":{\"id\":\"4218\"},\"glyph\":{\"id\":\"4219\"},\"group\":null,\"hover_glyph\":null,\"muted_glyph\":{\"id\":\"4221\"},\"nonselection_glyph\":{\"id\":\"4220\"},\"view\":{\"id\":\"4223\"}},\"id\":\"4222\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"fill_color\":{\"value\":\"navy\"},\"hatch_color\":{\"value\":\"navy\"},\"height\":{\"value\":0.5},\"line_color\":{\"value\":\"navy\"},\"right\":{\"field\":\"right\"},\"y\":{\"field\":\"y\"}},\"id\":\"4219\",\"type\":\"HBar\"},{\"attributes\":{},\"id\":\"4205\",\"type\":\"WheelZoomTool\"},{\"attributes\":{},\"id\":\"4201\",\"type\":\"BasicTicker\"},{\"attributes\":{},\"id\":\"4188\",\"type\":\"DataRange1d\"},{\"attributes\":{},\"id\":\"4190\",\"type\":\"DataRange1d\"},{\"attributes\":{},\"id\":\"4402\",\"type\":\"UnionRenderers\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.2},\"fill_color\":{\"value\":\"navy\"},\"hatch_alpha\":{\"value\":0.2},\"hatch_color\":{\"value\":\"navy\"},\"height\":{\"value\":0.5},\"line_alpha\":{\"value\":0.2},\"line_color\":{\"value\":\"navy\"},\"right\":{\"field\":\"right\"},\"y\":{\"field\":\"y\"}},\"id\":\"4221\",\"type\":\"HBar\"},{\"attributes\":{\"bottom_units\":\"screen\",\"coordinates\":null,\"fill_alpha\":0.5,\"fill_color\":\"lightgrey\",\"group\":null,\"left_units\":\"screen\",\"level\":\"overlay\",\"line_alpha\":1.0,\"line_color\":\"black\",\"line_dash\":[4,4],\"line_width\":2,\"right_units\":\"screen\",\"syncable\":false,\"top_units\":\"screen\"},\"id\":\"4210\",\"type\":\"BoxAnnotation\"},{\"attributes\":{},\"id\":\"4208\",\"type\":\"ResetTool\"}],\"root_ids\":[\"4185\"]},\"title\":\"Bokeh Application\",\"version\":\"2.4.2\"}};\n",
       "  const render_items = [{\"docid\":\"344867c2-1680-4cdb-b608-90f313afe6d5\",\"root_ids\":[\"4185\"],\"roots\":{\"4185\":\"2c072704-decb-414f-bd45-d049226b290b\"}}];\n",
       "  root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\n",
       "\n",
       "  }\n",
       "  if (root.Bokeh !== undefined) {\n",
       "    embed_document(root);\n",
       "  } else {\n",
       "    let attempts = 0;\n",
       "    const timer = setInterval(function(root) {\n",
       "      if (root.Bokeh !== undefined) {\n",
       "        clearInterval(timer);\n",
       "        embed_document(root);\n",
       "      } else {\n",
       "        attempts++;\n",
       "        if (attempts > 100) {\n",
       "          clearInterval(timer);\n",
       "          console.log(\"Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing\");\n",
       "        }\n",
       "      }\n",
       "    }, 10, root)\n",
       "  }\n",
       "})(window);"
      ],
      "application/vnd.bokehjs_exec.v0+json": ""
     },
     "metadata": {
      "application/vnd.bokehjs_exec.v0+json": {
       "id": "4185"
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 画布\n",
    "p = figure(plot_width=400,plot_height=400,title=\"水平柱状图Demo\")\n",
    "\n",
    "# 绘制\n",
    "p.hbar(\n",
    "    y=[1,2,3],\n",
    "    height=0.5,\n",
    "    left=0,\n",
    "    right=[1.2,2.5,3.7],\n",
    "    color=\"navy\"\n",
    ")\n",
    "show(p)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 案例DEMO(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "  <div class=\"bk-root\" id=\"4b3c0f8b-2130-4751-bd30-9a7e043ba083\" data-root-id=\"4456\"></div>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/javascript": [
       "(function(root) {\n",
       "  function embed_document(root) {\n",
       "    \n",
       "  const docs_json = {\"062e013c-8fdd-42c3-80b9-793af069d97c\":{\"defs\":[],\"roots\":{\"references\":[{\"attributes\":{\"below\":[{\"id\":\"4467\"}],\"center\":[{\"id\":\"4469\"},{\"id\":\"4473\"}],\"height\":350,\"left\":[{\"id\":\"4470\"}],\"renderers\":[{\"id\":\"4494\"}],\"title\":{\"id\":\"4457\"},\"toolbar\":{\"id\":\"4482\"},\"x_range\":{\"id\":\"4459\"},\"x_scale\":{\"id\":\"4463\"},\"y_range\":{\"id\":\"4461\"},\"y_scale\":{\"id\":\"4465\"}},\"id\":\"4456\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{\"callback\":null,\"tooltips\":[[\"count\",\"@counts\"]]},\"id\":\"4481\",\"type\":\"HoverTool\"},{\"attributes\":{\"axis\":{\"id\":\"4470\"},\"coordinates\":null,\"dimension\":1,\"group\":null,\"ticker\":null},\"id\":\"4473\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"4463\",\"type\":\"CategoricalScale\"},{\"attributes\":{},\"id\":\"4683\",\"type\":\"AllLabels\"},{\"attributes\":{\"coordinates\":null,\"data_source\":{\"id\":\"4490\"},\"glyph\":{\"id\":\"4491\"},\"group\":null,\"hover_glyph\":null,\"muted_glyph\":{\"id\":\"4493\"},\"nonselection_glyph\":{\"id\":\"4492\"},\"view\":{\"id\":\"4495\"}},\"id\":\"4494\",\"type\":\"GlyphRenderer\"},{\"attributes\":{},\"id\":\"4478\",\"type\":\"ResetTool\"},{\"attributes\":{\"coordinates\":null,\"group\":null,\"text\":\"Fruits Counts\"},\"id\":\"4457\",\"type\":\"Title\"},{\"attributes\":{\"coordinates\":null,\"formatter\":{\"id\":\"4682\"},\"group\":null,\"major_label_policy\":{\"id\":\"4683\"},\"ticker\":{\"id\":\"4468\"}},\"id\":\"4467\",\"type\":\"CategoricalAxis\"},{\"attributes\":{\"tools\":[{\"id\":\"4474\"},{\"id\":\"4475\"},{\"id\":\"4476\"},{\"id\":\"4477\"},{\"id\":\"4478\"},{\"id\":\"4479\"},{\"id\":\"4481\"}]},\"id\":\"4482\",\"type\":\"Toolbar\"},{\"attributes\":{},\"id\":\"4471\",\"type\":\"BasicTicker\"},{\"attributes\":{\"coordinates\":null,\"formatter\":{\"id\":\"4679\"},\"group\":null,\"major_label_policy\":{\"id\":\"4680\"},\"ticker\":{\"id\":\"4471\"}},\"id\":\"4470\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"4468\",\"type\":\"CategoricalTicker\"},{\"attributes\":{},\"id\":\"4479\",\"type\":\"HelpTool\"},{\"attributes\":{},\"id\":\"4474\",\"type\":\"PanTool\"},{\"attributes\":{\"axis\":{\"id\":\"4467\"},\"coordinates\":null,\"grid_line_color\":null,\"group\":null,\"ticker\":null},\"id\":\"4469\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"4679\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"overlay\":{\"id\":\"4480\"}},\"id\":\"4476\",\"type\":\"BoxZoomTool\"},{\"attributes\":{},\"id\":\"4685\",\"type\":\"Selection\"},{\"attributes\":{\"start\":0},\"id\":\"4461\",\"type\":\"DataRange1d\"},{\"attributes\":{},\"id\":\"4680\",\"type\":\"AllLabels\"},{\"attributes\":{\"data\":{\"top\":[5,3,4,2,4,6],\"x\":[\"Apples\",\"Pears\",\"Nectarines\",\"Plunms\",\"Grapes\",\"Straeberries\"]},\"selected\":{\"id\":\"4685\"},\"selection_policy\":{\"id\":\"4684\"}},\"id\":\"4490\",\"type\":\"ColumnDataSource\"},{\"attributes\":{},\"id\":\"4477\",\"type\":\"SaveTool\"},{\"attributes\":{},\"id\":\"4682\",\"type\":\"CategoricalTickFormatter\"},{\"attributes\":{\"source\":{\"id\":\"4490\"}},\"id\":\"4495\",\"type\":\"CDSView\"},{\"attributes\":{\"fill_color\":{\"value\":\"#1f77b4\"},\"line_color\":{\"value\":\"#1f77b4\"},\"top\":{\"field\":\"top\"},\"width\":{\"value\":0.5},\"x\":{\"field\":\"x\"}},\"id\":\"4491\",\"type\":\"VBar\"},{\"attributes\":{},\"id\":\"4465\",\"type\":\"LinearScale\"},{\"attributes\":{\"bottom_units\":\"screen\",\"coordinates\":null,\"fill_alpha\":0.5,\"fill_color\":\"lightgrey\",\"group\":null,\"left_units\":\"screen\",\"level\":\"overlay\",\"line_alpha\":1.0,\"line_color\":\"black\",\"line_dash\":[4,4],\"line_width\":2,\"right_units\":\"screen\",\"syncable\":false,\"top_units\":\"screen\"},\"id\":\"4480\",\"type\":\"BoxAnnotation\"},{\"attributes\":{\"factors\":[\"Apples\",\"Pears\",\"Nectarines\",\"Plunms\",\"Grapes\",\"Straeberries\"]},\"id\":\"4459\",\"type\":\"FactorRange\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.2},\"fill_color\":{\"value\":\"#1f77b4\"},\"hatch_alpha\":{\"value\":0.2},\"line_alpha\":{\"value\":0.2},\"line_color\":{\"value\":\"#1f77b4\"},\"top\":{\"field\":\"top\"},\"width\":{\"value\":0.5},\"x\":{\"field\":\"x\"}},\"id\":\"4493\",\"type\":\"VBar\"},{\"attributes\":{},\"id\":\"4475\",\"type\":\"WheelZoomTool\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.1},\"fill_color\":{\"value\":\"#1f77b4\"},\"hatch_alpha\":{\"value\":0.1},\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":\"#1f77b4\"},\"top\":{\"field\":\"top\"},\"width\":{\"value\":0.5},\"x\":{\"field\":\"x\"}},\"id\":\"4492\",\"type\":\"VBar\"},{\"attributes\":{},\"id\":\"4684\",\"type\":\"UnionRenderers\"}],\"root_ids\":[\"4456\"]},\"title\":\"Bokeh Application\",\"version\":\"2.4.2\"}};\n",
       "  const render_items = [{\"docid\":\"062e013c-8fdd-42c3-80b9-793af069d97c\",\"root_ids\":[\"4456\"],\"roots\":{\"4456\":\"4b3c0f8b-2130-4751-bd30-9a7e043ba083\"}}];\n",
       "  root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\n",
       "\n",
       "  }\n",
       "  if (root.Bokeh !== undefined) {\n",
       "    embed_document(root);\n",
       "  } else {\n",
       "    let attempts = 0;\n",
       "    const timer = setInterval(function(root) {\n",
       "      if (root.Bokeh !== undefined) {\n",
       "        clearInterval(timer);\n",
       "        embed_document(root);\n",
       "      } else {\n",
       "        attempts++;\n",
       "        if (attempts > 100) {\n",
       "          clearInterval(timer);\n",
       "          console.log(\"Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing\");\n",
       "        }\n",
       "      }\n",
       "    }, 10, root)\n",
       "  }\n",
       "})(window);"
      ],
      "application/vnd.bokehjs_exec.v0+json": ""
     },
     "metadata": {
      "application/vnd.bokehjs_exec.v0+json": {
       "id": "4456"
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from bokeh.models import ColumnDataSource\n",
    "\n",
    "# 数据准备（名称和值）\n",
    "fruits = ['Apples','Pears','Nectarines','Plunms','Grapes','Straeberries']\n",
    "counts = [5,3,4,2,4,6]\n",
    "\n",
    "# 数据库准备\n",
    "source = ColumnDataSource(\n",
    "    data = dict(\n",
    "        x = fruits,\n",
    "        counts = counts\n",
    "    )\n",
    ")\n",
    "\n",
    "TOOLTIPS = [\n",
    "    (\"count\",\"@counts\")\n",
    "]\n",
    "\n",
    "# 画布\n",
    "p = figure(\n",
    "    x_range=fruits,\n",
    "    plot_height=350,\n",
    "    title=\"Fruits Counts\",\n",
    "    tooltips=TOOLTIPS\n",
    ")\n",
    "\n",
    "# 绘制\n",
    "p.vbar(x=fruits,top=counts,width=0.5)\n",
    "\n",
    "p.xgrid.grid_line_color = None\n",
    "p.y_range.start = 0\n",
    "\n",
    "show(p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 学生实践：基本柱状图\n",
    "* 数据源:visualization-20190505.csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Visualization_tools</th>\n",
       "      <th>Watch</th>\n",
       "      <th>Star</th>\n",
       "      <th>Fork</th>\n",
       "      <th>Commits</th>\n",
       "      <th>Contributors</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>matplotlib</td>\n",
       "      <td>533</td>\n",
       "      <td>9678</td>\n",
       "      <td>4143</td>\n",
       "      <td>29503</td>\n",
       "      <td>808</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>bokeh</td>\n",
       "      <td>396</td>\n",
       "      <td>11034</td>\n",
       "      <td>2526</td>\n",
       "      <td>17673</td>\n",
       "      <td>357</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>pyecharts</td>\n",
       "      <td>263</td>\n",
       "      <td>5387</td>\n",
       "      <td>1148</td>\n",
       "      <td>1321</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>seaborn</td>\n",
       "      <td>234</td>\n",
       "      <td>6038</td>\n",
       "      <td>970</td>\n",
       "      <td>2316</td>\n",
       "      <td>98</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>plotly</td>\n",
       "      <td>234</td>\n",
       "      <td>4928</td>\n",
       "      <td>1114</td>\n",
       "      <td>3370</td>\n",
       "      <td>76</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>ggplot2</td>\n",
       "      <td>329</td>\n",
       "      <td>3783</td>\n",
       "      <td>1427</td>\n",
       "      <td>4286</td>\n",
       "      <td>184</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  Visualization_tools  Watch   Star  Fork  Commits  Contributors\n",
       "0          matplotlib    533   9678  4143    29503           808\n",
       "1               bokeh    396  11034  2526    17673           357\n",
       "2           pyecharts    263   5387  1148     1321            18\n",
       "3             seaborn    234   6038   970     2316            98\n",
       "4              plotly    234   4928  1114     3370            76\n",
       "5             ggplot2    329   3783  1427     4286           184"
      ]
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数据源准备\n",
    "import pandas as pd\n",
    "pd.read_csv('visualization-20190505.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     9678\n",
       "1    11034\n",
       "2     5387\n",
       "3     6038\n",
       "4     4928\n",
       "5     3783\n",
       "Name: Star, dtype: int64"
      ]
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv('visualization-20190505.csv')\n",
    "df['Star']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    matplotlib\n",
       "1         bokeh\n",
       "2     pyecharts\n",
       "3       seaborn\n",
       "4        plotly\n",
       "5       ggplot2\n",
       "Name: Visualization_tools, dtype: object"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['Visualization_tools']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "BokehDeprecationWarning: 'legend' keyword is deprecated, use explicit 'legend_label', 'legend_field', or 'legend_group' keywords instead\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "  <div class=\"bk-root\" id=\"ae3fed9b-3e1c-43b2-9bdf-7dce6e6fa563\" data-root-id=\"4738\"></div>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/javascript": [
       "(function(root) {\n",
       "  function embed_document(root) {\n",
       "    \n",
       "  const docs_json = {\"93cce77f-ad21-4a89-a185-a84f060aac32\":{\"defs\":[],\"roots\":{\"references\":[{\"attributes\":{\"below\":[{\"id\":\"4749\"}],\"center\":[{\"id\":\"4751\"},{\"id\":\"4755\"},{\"id\":\"4788\"}],\"height\":450,\"left\":[{\"id\":\"4752\"}],\"renderers\":[{\"id\":\"4776\"}],\"title\":{\"id\":\"4739\"},\"toolbar\":{\"id\":\"4764\"},\"x_range\":{\"id\":\"4741\"},\"x_scale\":{\"id\":\"4745\"},\"y_range\":{\"id\":\"4743\"},\"y_scale\":{\"id\":\"4747\"}},\"id\":\"4738\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{},\"id\":\"4747\",\"type\":\"LinearScale\"},{\"attributes\":{\"fill_color\":{\"field\":\"color\"},\"hatch_color\":{\"field\":\"color\"},\"line_color\":{\"field\":\"color\"},\"top\":{\"field\":\"star_counts\"},\"width\":{\"value\":0.8},\"x\":{\"field\":\"tools\"}},\"id\":\"4773\",\"type\":\"VBar\"},{\"attributes\":{},\"id\":\"4760\",\"type\":\"ResetTool\"},{\"attributes\":{},\"id\":\"4781\",\"type\":\"AllLabels\"},{\"attributes\":{},\"id\":\"4783\",\"type\":\"CategoricalTickFormatter\"},{\"attributes\":{\"end\":14000,\"start\":0},\"id\":\"4743\",\"type\":\"DataRange1d\"},{\"attributes\":{},\"id\":\"4786\",\"type\":\"Selection\"},{\"attributes\":{\"callback\":null,\"tooltips\":[[\"star\",\"@star_counts\"],[\"tools\",\"@tools\"]]},\"id\":\"4763\",\"type\":\"HoverTool\"},{\"attributes\":{\"tools\":[{\"id\":\"4756\"},{\"id\":\"4757\"},{\"id\":\"4758\"},{\"id\":\"4759\"},{\"id\":\"4760\"},{\"id\":\"4761\"},{\"id\":\"4763\"}]},\"id\":\"4764\",\"type\":\"Toolbar\"},{\"attributes\":{},\"id\":\"4784\",\"type\":\"AllLabels\"},{\"attributes\":{},\"id\":\"4756\",\"type\":\"PanTool\"},{\"attributes\":{\"coordinates\":null,\"group\":null,\"items\":[{\"id\":\"4789\"}],\"location\":\"top_center\",\"orientation\":\"horizontal\"},\"id\":\"4788\",\"type\":\"Legend\"},{\"attributes\":{},\"id\":\"4757\",\"type\":\"WheelZoomTool\"},{\"attributes\":{},\"id\":\"4750\",\"type\":\"CategoricalTicker\"},{\"attributes\":{\"coordinates\":null,\"data_source\":{\"id\":\"4737\"},\"glyph\":{\"id\":\"4773\"},\"group\":null,\"hover_glyph\":null,\"muted_glyph\":{\"id\":\"4775\"},\"nonselection_glyph\":{\"id\":\"4774\"},\"view\":{\"id\":\"4777\"}},\"id\":\"4776\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"label\":{\"field\":\"tools\"},\"renderers\":[{\"id\":\"4776\"}]},\"id\":\"4789\",\"type\":\"LegendItem\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.1},\"fill_color\":{\"field\":\"color\"},\"hatch_alpha\":{\"value\":0.1},\"hatch_color\":{\"field\":\"color\"},\"line_alpha\":{\"value\":0.1},\"line_color\":{\"field\":\"color\"},\"top\":{\"field\":\"star_counts\"},\"width\":{\"value\":0.8},\"x\":{\"field\":\"tools\"}},\"id\":\"4774\",\"type\":\"VBar\"},{\"attributes\":{\"coordinates\":null,\"group\":null,\"text\":\"Visualization_tools\"},\"id\":\"4739\",\"type\":\"Title\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.2},\"fill_color\":{\"field\":\"color\"},\"hatch_alpha\":{\"value\":0.2},\"hatch_color\":{\"field\":\"color\"},\"line_alpha\":{\"value\":0.2},\"line_color\":{\"field\":\"color\"},\"top\":{\"field\":\"star_counts\"},\"width\":{\"value\":0.8},\"x\":{\"field\":\"tools\"}},\"id\":\"4775\",\"type\":\"VBar\"},{\"attributes\":{\"axis\":{\"id\":\"4752\"},\"coordinates\":null,\"dimension\":1,\"group\":null,\"ticker\":null},\"id\":\"4755\",\"type\":\"Grid\"},{\"attributes\":{\"axis\":{\"id\":\"4749\"},\"coordinates\":null,\"grid_line_color\":null,\"group\":null,\"ticker\":null},\"id\":\"4751\",\"type\":\"Grid\"},{\"attributes\":{\"overlay\":{\"id\":\"4762\"}},\"id\":\"4758\",\"type\":\"BoxZoomTool\"},{\"attributes\":{\"coordinates\":null,\"formatter\":{\"id\":\"4783\"},\"group\":null,\"major_label_policy\":{\"id\":\"4784\"},\"ticker\":{\"id\":\"4750\"}},\"id\":\"4749\",\"type\":\"CategoricalAxis\"},{\"attributes\":{\"source\":{\"id\":\"4737\"}},\"id\":\"4777\",\"type\":\"CDSView\"},{\"attributes\":{},\"id\":\"4761\",\"type\":\"HelpTool\"},{\"attributes\":{\"data\":{\"color\":[\"#3288bd\",\"#99d594\",\"#e6f598\",\"#fee08b\",\"#fc8d59\",\"#d53e4f\"],\"star_counts\":[9678,11034,5387,6038,4928,3783],\"tools\":[\"matplotlib\",\"bokeh\",\"pyecharts\",\"seaborn\",\"plotly\",\"ggplot2\"]},\"selected\":{\"id\":\"4786\"},\"selection_policy\":{\"id\":\"4785\"}},\"id\":\"4737\",\"type\":\"ColumnDataSource\"},{\"attributes\":{\"bottom_units\":\"screen\",\"coordinates\":null,\"fill_alpha\":0.5,\"fill_color\":\"lightgrey\",\"group\":null,\"left_units\":\"screen\",\"level\":\"overlay\",\"line_alpha\":1.0,\"line_color\":\"black\",\"line_dash\":[4,4],\"line_width\":2,\"right_units\":\"screen\",\"syncable\":false,\"top_units\":\"screen\"},\"id\":\"4762\",\"type\":\"BoxAnnotation\"},{\"attributes\":{\"coordinates\":null,\"formatter\":{\"id\":\"4780\"},\"group\":null,\"major_label_policy\":{\"id\":\"4781\"},\"ticker\":{\"id\":\"4753\"}},\"id\":\"4752\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"4785\",\"type\":\"UnionRenderers\"},{\"attributes\":{},\"id\":\"4759\",\"type\":\"SaveTool\"},{\"attributes\":{},\"id\":\"4780\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"factors\":[\"matplotlib\",\"bokeh\",\"pyecharts\",\"seaborn\",\"plotly\",\"ggplot2\"]},\"id\":\"4741\",\"type\":\"FactorRange\"},{\"attributes\":{},\"id\":\"4753\",\"type\":\"BasicTicker\"},{\"attributes\":{},\"id\":\"4745\",\"type\":\"CategoricalScale\"}],\"root_ids\":[\"4738\"]},\"title\":\"Bokeh Application\",\"version\":\"2.4.2\"}};\n",
       "  const render_items = [{\"docid\":\"93cce77f-ad21-4a89-a185-a84f060aac32\",\"root_ids\":[\"4738\"],\"roots\":{\"4738\":\"ae3fed9b-3e1c-43b2-9bdf-7dce6e6fa563\"}}];\n",
       "  root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\n",
       "\n",
       "  }\n",
       "  if (root.Bokeh !== undefined) {\n",
       "    embed_document(root);\n",
       "  } else {\n",
       "    let attempts = 0;\n",
       "    const timer = setInterval(function(root) {\n",
       "      if (root.Bokeh !== undefined) {\n",
       "        clearInterval(timer);\n",
       "        embed_document(root);\n",
       "      } else {\n",
       "        attempts++;\n",
       "        if (attempts > 100) {\n",
       "          clearInterval(timer);\n",
       "          console.log(\"Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing\");\n",
       "        }\n",
       "      }\n",
       "    }, 10, root)\n",
       "  }\n",
       "})(window);"
      ],
      "application/vnd.bokehjs_exec.v0+json": ""
     },
     "metadata": {
      "application/vnd.bokehjs_exec.v0+json": {
       "id": "4738"
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from bokeh.models import ColumnDataSource\n",
    "from bokeh.palettes import Spectral6\n",
    "\n",
    "\n",
    "# 数据库准备\n",
    "source = ColumnDataSource(\n",
    "    data = dict(\n",
    "        tools = df['Visualization_tools'],\n",
    "        star_counts = df['Star'],\n",
    "        color = Spectral6  # 坑，已解决，颜色也应当存进source\n",
    "    )\n",
    ")\n",
    "\n",
    "TOOLTIPS = [\n",
    "    (\"star\",\"@star_counts\"),\n",
    "    (\"tools\",\"@tools\")\n",
    "]\n",
    "\n",
    "# 画布\n",
    "p = figure(\n",
    "    x_range=df['Visualization_tools'],\n",
    "    plot_height=450,\n",
    "    title=\"Visualization_tools\",\n",
    "    tooltips=TOOLTIPS\n",
    ")\n",
    "\n",
    "# 绘制\n",
    "p.vbar(x='tools',top='star_counts',color='color',width=0.8,source=source,legend=\"tools\")\n",
    "\n",
    "p.xgrid.grid_line_color = None\n",
    "p.y_range.start = 0\n",
    "p.legend.location='top_center'\n",
    "p.legend.orientation=\"horizontal\"\n",
    "p.y_range.end = 14000\n",
    "show(p)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 分组柱状图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "  <div class=\"bk-root\" id=\"dc602f53-2e99-4cef-a48c-863cb755b40b\" data-root-id=\"5043\"></div>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/javascript": [
       "(function(root) {\n",
       "  function embed_document(root) {\n",
       "    \n",
       "  const docs_json = {\"d6896b8f-2066-4a18-9d3f-ec9442c8af8a\":{\"defs\":[],\"roots\":{\"references\":[{\"attributes\":{\"below\":[{\"id\":\"5053\"}],\"center\":[{\"id\":\"5055\"},{\"id\":\"5059\"}],\"height\":350,\"left\":[{\"id\":\"5056\"}],\"renderers\":[{\"id\":\"5065\"}],\"title\":{\"id\":\"5044\"},\"toolbar\":{\"id\":\"5060\"},\"toolbar_location\":null,\"x_range\":{\"id\":\"5042\"},\"x_scale\":{\"id\":\"5049\"},\"y_range\":{\"id\":\"5047\"},\"y_scale\":{\"id\":\"5051\"}},\"id\":\"5043\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{\"start\":0},\"id\":\"5047\",\"type\":\"DataRange1d\"},{\"attributes\":{\"coordinates\":null,\"data_source\":{\"id\":\"5041\"},\"glyph\":{\"id\":\"5062\"},\"group\":null,\"hover_glyph\":null,\"muted_glyph\":{\"id\":\"5064\"},\"nonselection_glyph\":{\"id\":\"5063\"},\"view\":{\"id\":\"5066\"}},\"id\":\"5065\",\"type\":\"GlyphRenderer\"},{\"attributes\":{},\"id\":\"5049\",\"type\":\"CategoricalScale\"},{\"attributes\":{},\"id\":\"5271\",\"type\":\"AllLabels\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.2},\"fill_color\":{\"value\":\"#1f77b4\"},\"hatch_alpha\":{\"value\":0.2},\"line_alpha\":{\"value\":0.2},\"line_color\":{\"value\":\"#1f77b4\"},\"top\":{\"field\":\"counts\"},\"width\":{\"value\":0.9},\"x\":{\"field\":\"x\"}},\"id\":\"5064\",\"type\":\"VBar\"},{\"attributes\":{},\"id\":\"5275\",\"type\":\"UnionRenderers\"},{\"attributes\":{},\"id\":\"5270\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{},\"id\":\"5060\",\"type\":\"Toolbar\"},{\"attributes\":{},\"id\":\"5051\",\"type\":\"LinearScale\"},{\"attributes\":{\"axis\":{\"id\":\"5053\"},\"coordinates\":null,\"grid_line_color\":null,\"group\":null,\"ticker\":null},\"id\":\"5055\",\"type\":\"Grid\"},{\"attributes\":{\"coordinates\":null,\"formatter\":{\"id\":\"5273\"},\"group\":null,\"major_label_orientation\":1,\"major_label_policy\":{\"id\":\"5274\"},\"ticker\":{\"id\":\"5054\"}},\"id\":\"5053\",\"type\":\"CategoricalAxis\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.1},\"fill_color\":{\"value\":\"#1f77b4\"},\"hatch_alpha\":{\"value\":0.1},\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":\"#1f77b4\"},\"top\":{\"field\":\"counts\"},\"width\":{\"value\":0.9},\"x\":{\"field\":\"x\"}},\"id\":\"5063\",\"type\":\"VBar\"},{\"attributes\":{\"source\":{\"id\":\"5041\"}},\"id\":\"5066\",\"type\":\"CDSView\"},{\"attributes\":{},\"id\":\"5273\",\"type\":\"CategoricalTickFormatter\"},{\"attributes\":{\"coordinates\":null,\"formatter\":{\"id\":\"5270\"},\"group\":null,\"major_label_policy\":{\"id\":\"5271\"},\"ticker\":{\"id\":\"5057\"}},\"id\":\"5056\",\"type\":\"LinearAxis\"},{\"attributes\":{\"data\":{\"counts\":[2,5,3,1,3,2,4,3,4,3,2,4,2,4,5,4,6,3],\"x\":[[\"Apples\",\"2015\"],[\"Apples\",\"2016\"],[\"Apples\",\"2017\"],[\"Pears\",\"2015\"],[\"Pears\",\"2016\"],[\"Pears\",\"2017\"],[\"Nectarines\",\"2015\"],[\"Nectarines\",\"2016\"],[\"Nectarines\",\"2017\"],[\"Plunms\",\"2015\"],[\"Plunms\",\"2016\"],[\"Plunms\",\"2017\"],[\"Grapes\",\"2015\"],[\"Grapes\",\"2016\"],[\"Grapes\",\"2017\"],[\"Straeberries\",\"2015\"],[\"Straeberries\",\"2016\"],[\"Straeberries\",\"2017\"]]},\"selected\":{\"id\":\"5276\"},\"selection_policy\":{\"id\":\"5275\"}},\"id\":\"5041\",\"type\":\"ColumnDataSource\"},{\"attributes\":{\"coordinates\":null,\"group\":null,\"text\":\"Fruit Counts by Year\"},\"id\":\"5044\",\"type\":\"Title\"},{\"attributes\":{\"axis\":{\"id\":\"5056\"},\"coordinates\":null,\"dimension\":1,\"group\":null,\"ticker\":null},\"id\":\"5059\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"5276\",\"type\":\"Selection\"},{\"attributes\":{},\"id\":\"5057\",\"type\":\"BasicTicker\"},{\"attributes\":{\"factors\":[[\"Apples\",\"2015\"],[\"Apples\",\"2016\"],[\"Apples\",\"2017\"],[\"Pears\",\"2015\"],[\"Pears\",\"2016\"],[\"Pears\",\"2017\"],[\"Nectarines\",\"2015\"],[\"Nectarines\",\"2016\"],[\"Nectarines\",\"2017\"],[\"Plunms\",\"2015\"],[\"Plunms\",\"2016\"],[\"Plunms\",\"2017\"],[\"Grapes\",\"2015\"],[\"Grapes\",\"2016\"],[\"Grapes\",\"2017\"],[\"Straeberries\",\"2015\"],[\"Straeberries\",\"2016\"],[\"Straeberries\",\"2017\"]],\"range_padding\":0.1},\"id\":\"5042\",\"type\":\"FactorRange\"},{\"attributes\":{},\"id\":\"5054\",\"type\":\"CategoricalTicker\"},{\"attributes\":{\"fill_color\":{\"value\":\"#1f77b4\"},\"line_color\":{\"value\":\"#1f77b4\"},\"top\":{\"field\":\"counts\"},\"width\":{\"value\":0.9},\"x\":{\"field\":\"x\"}},\"id\":\"5062\",\"type\":\"VBar\"},{\"attributes\":{},\"id\":\"5274\",\"type\":\"AllLabels\"}],\"root_ids\":[\"5043\"]},\"title\":\"Bokeh Application\",\"version\":\"2.4.2\"}};\n",
       "  const render_items = [{\"docid\":\"d6896b8f-2066-4a18-9d3f-ec9442c8af8a\",\"root_ids\":[\"5043\"],\"roots\":{\"5043\":\"dc602f53-2e99-4cef-a48c-863cb755b40b\"}}];\n",
       "  root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\n",
       "\n",
       "  }\n",
       "  if (root.Bokeh !== undefined) {\n",
       "    embed_document(root);\n",
       "  } else {\n",
       "    let attempts = 0;\n",
       "    const timer = setInterval(function(root) {\n",
       "      if (root.Bokeh !== undefined) {\n",
       "        clearInterval(timer);\n",
       "        embed_document(root);\n",
       "      } else {\n",
       "        attempts++;\n",
       "        if (attempts > 100) {\n",
       "          clearInterval(timer);\n",
       "          console.log(\"Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing\");\n",
       "        }\n",
       "      }\n",
       "    }, 10, root)\n",
       "  }\n",
       "})(window);"
      ],
      "application/vnd.bokehjs_exec.v0+json": ""
     },
     "metadata": {
      "application/vnd.bokehjs_exec.v0+json": {
       "id": "5043"
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from bokeh.models import ColumnDataSource, FactorRange\n",
    "fruits = ['Apples','Pears','Nectarines','Plunms','Grapes','Straeberries']\n",
    "years = ['2015','2016','2017']\n",
    "data = {'fruits' : fruits,\n",
    "        '2015':[2,1,4,3,2,4],\n",
    "        '2016':[5,3,3,2,4,6],\n",
    "        '2017':[3,2,4,4,5,3]}\n",
    "\n",
    "# 创建复合列表[(\"Apples\",\"2015\"),(\"Apples\",\"2016\"),(\"Apples\",\"2017\")……]\n",
    "x = [(fruit, year) for fruit in fruits for year in years ]\n",
    "counts = sum(zip(data['2015'],data['2016'],data['2017']),()) # 分组求和（堆叠总数）\n",
    "source = ColumnDataSource(data=dict(x=x,counts=counts))\n",
    "\n",
    "# 画布\n",
    "p = figure(x_range=FactorRange(*x), height=350, title=\"Fruit Counts by Year\",\n",
    "           toolbar_location=None, tools=\"\")\n",
    "\n",
    "# 柱状图\n",
    "p.vbar(x='x',top='counts',width=0.9,source=source)\n",
    "# 其他\n",
    "p.y_range.start = 0\n",
    "p.x_range.range_padding = 0.1\n",
    "p.xaxis.major_label_orientation = 1\n",
    "p.xgrid.grid_line_color = None\n",
    "# 显示\n",
    "show(p)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 课后实践(star,commits,contributes) 分组柱状图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Visualization_tools</th>\n",
       "      <th>Watch</th>\n",
       "      <th>Star</th>\n",
       "      <th>Fork</th>\n",
       "      <th>Commits</th>\n",
       "      <th>Contributors</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>matplotlib</td>\n",
       "      <td>533</td>\n",
       "      <td>9678</td>\n",
       "      <td>4143</td>\n",
       "      <td>29503</td>\n",
       "      <td>808</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>bokeh</td>\n",
       "      <td>396</td>\n",
       "      <td>11034</td>\n",
       "      <td>2526</td>\n",
       "      <td>17673</td>\n",
       "      <td>357</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>pyecharts</td>\n",
       "      <td>263</td>\n",
       "      <td>5387</td>\n",
       "      <td>1148</td>\n",
       "      <td>1321</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>seaborn</td>\n",
       "      <td>234</td>\n",
       "      <td>6038</td>\n",
       "      <td>970</td>\n",
       "      <td>2316</td>\n",
       "      <td>98</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>plotly</td>\n",
       "      <td>234</td>\n",
       "      <td>4928</td>\n",
       "      <td>1114</td>\n",
       "      <td>3370</td>\n",
       "      <td>76</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>ggplot2</td>\n",
       "      <td>329</td>\n",
       "      <td>3783</td>\n",
       "      <td>1427</td>\n",
       "      <td>4286</td>\n",
       "      <td>184</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  Visualization_tools  Watch   Star  Fork  Commits  Contributors\n",
       "0          matplotlib    533   9678  4143    29503           808\n",
       "1               bokeh    396  11034  2526    17673           357\n",
       "2           pyecharts    263   5387  1148     1321            18\n",
       "3             seaborn    234   6038   970     2316            98\n",
       "4              plotly    234   4928  1114     3370            76\n",
       "5             ggplot2    329   3783  1427     4286           184"
      ]
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数据源准备\n",
    "import pandas as pd\n",
    "pd.read_csv('visualization-20190505.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "data_tools = pd.read_csv('visualization-20190505.csv',usecols = [0]) #读取第一列的数据\n",
    "data_array_tools = np.array(data_tools.stack()) # 首先将pandas读取的数据转化为array\n",
    "data_list_tools = data_array_tools.tolist() # 然后转化为list形式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['matplotlib', 'bokeh', 'pyecharts', 'seaborn', 'plotly', 'ggplot2']"
      ]
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_list_tools"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_Star = pd.read_csv('visualization-20190505.csv',usecols = [2]) #读取第二列的数据\n",
    "data_array_Star = np.array(data_Star.stack()) # 首先将pandas读取的数据转化为array\n",
    "data_list_Star = data_array_Star.tolist() # 然后转化为list形式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 149,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[9678, 11034, 5387, 6038, 4928, 3783]"
      ]
     },
     "execution_count": 149,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_list_Star"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 158,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_Commits = pd.read_csv('visualization-20190505.csv',usecols = [4]) #读取第二列的数据\n",
    "data_array_Commits = np.array(data_Commits.stack()) # 首先将pandas读取的数据转化为array\n",
    "data_list_Commits = data_array_Commits.tolist() # 然后转化为list形式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 159,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[29503, 17673, 1321, 2316, 3370, 4286]"
      ]
     },
     "execution_count": 159,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_list_Commits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 160,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_Contributors = pd.read_csv('visualization-20190505.csv',usecols = [5]) #读取第二列的数据\n",
    "data_array_Contributors = np.array(data_Contributors.stack()) # 首先将pandas读取的数据转化为array\n",
    "data_list_Contributors = data_array_Contributors.tolist() # 然后转化为list形式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 161,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[808, 357, 18, 98, 76, 184]"
      ]
     },
     "execution_count": 161,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_list_Contributors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 162,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "  <div class=\"bk-root\" id=\"6b0f4f61-4f8d-4729-bc23-77f31553db80\" data-root-id=\"6542\"></div>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/javascript": [
       "(function(root) {\n",
       "  function embed_document(root) {\n",
       "    \n",
       "  const docs_json = {\"5121ceb1-015c-4a04-8157-0836e31d9e6e\":{\"defs\":[],\"roots\":{\"references\":[{\"attributes\":{\"below\":[{\"id\":\"6552\"}],\"center\":[{\"id\":\"6554\"},{\"id\":\"6558\"}],\"height\":350,\"left\":[{\"id\":\"6555\"}],\"renderers\":[{\"id\":\"6564\"}],\"title\":{\"id\":\"6543\"},\"toolbar\":{\"id\":\"6559\"},\"toolbar_location\":null,\"x_range\":{\"id\":\"6541\"},\"x_scale\":{\"id\":\"6548\"},\"y_range\":{\"id\":\"6546\"},\"y_scale\":{\"id\":\"6550\"}},\"id\":\"6542\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{},\"id\":\"6553\",\"type\":\"CategoricalTicker\"},{\"attributes\":{},\"id\":\"6559\",\"type\":\"Toolbar\"},{\"attributes\":{\"factors\":[[\"matplotlib\",\"Star\"],[\"matplotlib\",\"Commits\"],[\"matplotlib\",\"Contributors\"],[\"bokeh\",\"Star\"],[\"bokeh\",\"Commits\"],[\"bokeh\",\"Contributors\"],[\"pyecharts\",\"Star\"],[\"pyecharts\",\"Commits\"],[\"pyecharts\",\"Contributors\"],[\"seaborn\",\"Star\"],[\"seaborn\",\"Commits\"],[\"seaborn\",\"Contributors\"],[\"plotly\",\"Star\"],[\"plotly\",\"Commits\"],[\"plotly\",\"Contributors\"],[\"ggplot2\",\"Star\"],[\"ggplot2\",\"Commits\"],[\"ggplot2\",\"Contributors\"]],\"range_padding\":0.1},\"id\":\"6541\",\"type\":\"FactorRange\"},{\"attributes\":{\"coordinates\":null,\"group\":null,\"text\":\"TOOLS Counts by Standard\"},\"id\":\"6543\",\"type\":\"Title\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.1},\"fill_color\":{\"value\":\"#1f77b4\"},\"hatch_alpha\":{\"value\":0.1},\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":\"#1f77b4\"},\"top\":{\"field\":\"counts\"},\"width\":{\"value\":0.9},\"x\":{\"field\":\"x\"}},\"id\":\"6562\",\"type\":\"VBar\"},{\"attributes\":{\"axis\":{\"id\":\"6555\"},\"coordinates\":null,\"dimension\":1,\"group\":null,\"ticker\":null},\"id\":\"6558\",\"type\":\"Grid\"},{\"attributes\":{\"start\":0},\"id\":\"6546\",\"type\":\"DataRange1d\"},{\"attributes\":{\"data\":{\"counts\":[9678,29503,808,11034,17673,357,5387,1321,18,6038,2316,98,4928,3370,76,3783,4286,184],\"x\":[[\"matplotlib\",\"Star\"],[\"matplotlib\",\"Commits\"],[\"matplotlib\",\"Contributors\"],[\"bokeh\",\"Star\"],[\"bokeh\",\"Commits\"],[\"bokeh\",\"Contributors\"],[\"pyecharts\",\"Star\"],[\"pyecharts\",\"Commits\"],[\"pyecharts\",\"Contributors\"],[\"seaborn\",\"Star\"],[\"seaborn\",\"Commits\"],[\"seaborn\",\"Contributors\"],[\"plotly\",\"Star\"],[\"plotly\",\"Commits\"],[\"plotly\",\"Contributors\"],[\"ggplot2\",\"Star\"],[\"ggplot2\",\"Commits\"],[\"ggplot2\",\"Contributors\"]]},\"selected\":{\"id\":\"6820\"},\"selection_policy\":{\"id\":\"6819\"}},\"id\":\"6540\",\"type\":\"ColumnDataSource\"},{\"attributes\":{},\"id\":\"6814\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"fill_color\":{\"value\":\"#1f77b4\"},\"line_color\":{\"value\":\"#1f77b4\"},\"top\":{\"field\":\"counts\"},\"width\":{\"value\":0.9},\"x\":{\"field\":\"x\"}},\"id\":\"6561\",\"type\":\"VBar\"},{\"attributes\":{\"source\":{\"id\":\"6540\"}},\"id\":\"6565\",\"type\":\"CDSView\"},{\"attributes\":{\"coordinates\":null,\"data_source\":{\"id\":\"6540\"},\"glyph\":{\"id\":\"6561\"},\"group\":null,\"hover_glyph\":null,\"muted_glyph\":{\"id\":\"6563\"},\"nonselection_glyph\":{\"id\":\"6562\"},\"view\":{\"id\":\"6565\"}},\"id\":\"6564\",\"type\":\"GlyphRenderer\"},{\"attributes\":{},\"id\":\"6818\",\"type\":\"AllLabels\"},{\"attributes\":{},\"id\":\"6819\",\"type\":\"UnionRenderers\"},{\"attributes\":{\"axis\":{\"id\":\"6552\"},\"coordinates\":null,\"grid_line_color\":null,\"group\":null,\"ticker\":null},\"id\":\"6554\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"6817\",\"type\":\"CategoricalTickFormatter\"},{\"attributes\":{},\"id\":\"6556\",\"type\":\"BasicTicker\"},{\"attributes\":{\"coordinates\":null,\"formatter\":{\"id\":\"6814\"},\"group\":null,\"major_label_policy\":{\"id\":\"6815\"},\"ticker\":{\"id\":\"6556\"}},\"id\":\"6555\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"6550\",\"type\":\"LinearScale\"},{\"attributes\":{},\"id\":\"6815\",\"type\":\"AllLabels\"},{\"attributes\":{\"coordinates\":null,\"formatter\":{\"id\":\"6817\"},\"group\":null,\"major_label_orientation\":1,\"major_label_policy\":{\"id\":\"6818\"},\"ticker\":{\"id\":\"6553\"}},\"id\":\"6552\",\"type\":\"CategoricalAxis\"},{\"attributes\":{},\"id\":\"6548\",\"type\":\"CategoricalScale\"},{\"attributes\":{},\"id\":\"6820\",\"type\":\"Selection\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.2},\"fill_color\":{\"value\":\"#1f77b4\"},\"hatch_alpha\":{\"value\":0.2},\"line_alpha\":{\"value\":0.2},\"line_color\":{\"value\":\"#1f77b4\"},\"top\":{\"field\":\"counts\"},\"width\":{\"value\":0.9},\"x\":{\"field\":\"x\"}},\"id\":\"6563\",\"type\":\"VBar\"}],\"root_ids\":[\"6542\"]},\"title\":\"Bokeh Application\",\"version\":\"2.4.2\"}};\n",
       "  const render_items = [{\"docid\":\"5121ceb1-015c-4a04-8157-0836e31d9e6e\",\"root_ids\":[\"6542\"],\"roots\":{\"6542\":\"6b0f4f61-4f8d-4729-bc23-77f31553db80\"}}];\n",
       "  root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\n",
       "\n",
       "  }\n",
       "  if (root.Bokeh !== undefined) {\n",
       "    embed_document(root);\n",
       "  } else {\n",
       "    let attempts = 0;\n",
       "    const timer = setInterval(function(root) {\n",
       "      if (root.Bokeh !== undefined) {\n",
       "        clearInterval(timer);\n",
       "        embed_document(root);\n",
       "      } else {\n",
       "        attempts++;\n",
       "        if (attempts > 100) {\n",
       "          clearInterval(timer);\n",
       "          console.log(\"Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing\");\n",
       "        }\n",
       "      }\n",
       "    }, 10, root)\n",
       "  }\n",
       "})(window);"
      ],
      "application/vnd.bokehjs_exec.v0+json": ""
     },
     "metadata": {
      "application/vnd.bokehjs_exec.v0+json": {
       "id": "6542"
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from bokeh.models import ColumnDataSource, FactorRange\n",
    "tools = data_list_tools\n",
    "standards = ['Star','Commits','Contributors']\n",
    "data = {'tools' : data_list_tools,\n",
    "        'Star':data_list_Star,\n",
    "        'Commits':data_list_Commits,\n",
    "        'Contributors':data_list_Contributors}\n",
    "\n",
    "# 创建复合列表[(\"Apples\",\"2015\"),(\"Apples\",\"2016\"),(\"Apples\",\"2017\")……]\n",
    "x = [(tool, standard) for tool in tools for standard in standards ]\n",
    "counts = sum(zip(data['Star'],data['Commits'],data['Contributors']),()) # 分组求和（堆叠总数）\n",
    "source = ColumnDataSource(data=dict(x=x,counts=counts))\n",
    "\n",
    "# 画布\n",
    "p = figure(x_range=FactorRange(*x), height=350, title=\"TOOLS Counts by Standard\",\n",
    "           toolbar_location=None, tools=\"\")\n",
    "\n",
    "# 柱状图\n",
    "p.vbar(x='x',top='counts',width=0.9,source=source)\n",
    "# 其他\n",
    "p.y_range.start = 0\n",
    "p.x_range.range_padding = 0.1\n",
    "p.xaxis.major_label_orientation = 1\n",
    "p.xgrid.grid_line_color = None\n",
    "# 显示\n",
    "show(p)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6rc1"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
